@echo off
chcp 65001 >nul
echo 正在设置WVP数据库...

REM 检查MySQL是否运行
echo 检查MySQL服务状态...
netstat -an | findstr ":3306" >nul
if %errorlevel% neq 0 (
    echo ❌ MySQL服务未运行，请先启动MySQL服务
    pause
    exit /b 1
)
echo ✓ MySQL服务正在运行

REM 创建SQL脚本
echo 正在生成数据库初始化脚本...
(
echo -- WVP数据库初始化脚本
echo CREATE DATABASE IF NOT EXISTS wvp DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
echo USE wvp;
echo.
echo -- 创建用户角色表
echo CREATE TABLE IF NOT EXISTS wvp_user_role ^(
echo     id INT AUTO_INCREMENT PRIMARY KEY,
echo     name VARCHAR^(255^) NOT NULL,
echo     authority VARCHAR^(255^),
echo     create_time VARCHAR^(50^),
echo     update_time VARCHAR^(50^)
echo ^);
echo.
echo -- 创建用户表
echo CREATE TABLE IF NOT EXISTS wvp_user ^(
echo     id INT AUTO_INCREMENT PRIMARY KEY,
echo     username VARCHAR^(255^) NOT NULL UNIQUE,
echo     password VARCHAR^(255^) NOT NULL,
echo     role_id INT DEFAULT 1,
echo     create_time VARCHAR^(50^),
echo     update_time VARCHAR^(50^),
echo     push_key VARCHAR^(50^)
echo ^);
echo.
echo -- 创建媒体服务器表
echo CREATE TABLE IF NOT EXISTS wvp_media_server ^(
echo     id VARCHAR^(255^) PRIMARY KEY,
echo     ip VARCHAR^(50^) NOT NULL,
echo     hook_ip VARCHAR^(50^),
echo     sdp_ip VARCHAR^(50^),
echo     stream_ip VARCHAR^(50^),
echo     http_port INT,
echo     http_ssl_port INT,
echo     rtmp_port INT,
echo     rtmp_ssl_port INT,
echo     rtp_proxy_port INT,
echo     rtsp_port INT,
echo     rtsp_ssl_port INT,
echo     auto_config TINYINT^(1^) DEFAULT 1,
echo     secret VARCHAR^(50^),
echo     rtp_enable TINYINT^(1^) DEFAULT 0,
echo     rtp_port_range VARCHAR^(50^),
echo     record_assist_port INT,
echo     default_server TINYINT^(1^) DEFAULT 0,
echo     create_time VARCHAR^(50^),
echo     update_time VARCHAR^(50^),
echo     last_keepalive_time VARCHAR^(50^),
echo     status TINYINT^(1^) DEFAULT 0,
echo     send_rtp_port_range VARCHAR^(50^),
echo     record_day INT DEFAULT 7,
echo     server_id VARCHAR^(50^)
echo ^);
echo.
echo -- 创建平台表
echo CREATE TABLE IF NOT EXISTS wvp_platform ^(
echo     id INT AUTO_INCREMENT PRIMARY KEY,
echo     enable TINYINT^(1^) DEFAULT 0,
echo     name VARCHAR^(255^),
echo     server_gb_id VARCHAR^(50^),
echo     server_gb_domain VARCHAR^(50^),
echo     server_ip VARCHAR^(50^),
echo     server_port INT,
echo     device_gb_id VARCHAR^(50^),
echo     device_ip VARCHAR^(50^),
echo     device_port VARCHAR^(50^),
echo     username VARCHAR^(255^),
echo     password VARCHAR^(255^),
echo     expires VARCHAR^(50^),
echo     keep_timeout VARCHAR^(50^),
echo     transport VARCHAR^(50^),
echo     character_set VARCHAR^(50^),
echo     catalog_id VARCHAR^(50^),
echo     ptz TINYINT^(1^) DEFAULT 0,
echo     rtcp TINYINT^(1^) DEFAULT 0,
echo     status TINYINT^(1^) DEFAULT 0,
echo     start_offline_push TINYINT^(1^) DEFAULT 0,
echo     catalog_group INT DEFAULT 1,
echo     administrative_division VARCHAR^(50^),
echo     create_time VARCHAR^(50^),
echo     update_time VARCHAR^(50^),
echo     as_message_channel TINYINT^(1^) DEFAULT 0,
echo     server_id VARCHAR^(50^)
echo ^);
echo.
echo -- 创建设备表
echo CREATE TABLE IF NOT EXISTS wvp_device ^(
echo     id INT AUTO_INCREMENT PRIMARY KEY,
echo     device_id VARCHAR^(50^) NOT NULL UNIQUE,
echo     name VARCHAR^(255^),
echo     manufacturer VARCHAR^(255^),
echo     model VARCHAR^(255^),
echo     firmware VARCHAR^(255^),
echo     transport VARCHAR^(50^),
echo     stream_mode VARCHAR^(50^),
echo     on_line TINYINT^(1^) DEFAULT 0,
echo     register_time VARCHAR^(50^),
echo     keepalive_time VARCHAR^(50^),
echo     ip VARCHAR^(50^),
echo     create_time VARCHAR^(50^),
echo     update_time VARCHAR^(50^),
echo     port INT,
echo     expires INT,
echo     host_address VARCHAR^(50^),
echo     charset VARCHAR^(50^),
echo     media_server_id VARCHAR^(50^) DEFAULT 'auto',
echo     password VARCHAR^(255^),
echo     server_id VARCHAR^(50^)
echo ^);
echo.
echo -- 创建设备通道表
echo CREATE TABLE IF NOT EXISTS wvp_device_channel ^(
echo     id INT AUTO_INCREMENT PRIMARY KEY,
echo     channel_id VARCHAR^(50^) NOT NULL,
echo     device_id VARCHAR^(50^) NOT NULL,
echo     name VARCHAR^(255^),
echo     manufacture VARCHAR^(255^),
echo     model VARCHAR^(255^),
echo     owner VARCHAR^(255^),
echo     civil_code VARCHAR^(255^),
echo     block VARCHAR^(255^),
echo     address VARCHAR^(255^),
echo     parent_id VARCHAR^(50^),
echo     safety_way INT,
echo     register_way INT,
echo     cert_num VARCHAR^(255^),
echo     certifiable INT,
echo     err_code INT,
echo     end_time VARCHAR^(255^),
echo     secrecy VARCHAR^(255^),
echo     ip_address VARCHAR^(255^),
echo     port INT,
echo     password VARCHAR^(255^),
echo     ptz_type INT,
echo     status TINYINT^(1^) DEFAULT 0,
echo     longitude DOUBLE,
echo     latitude DOUBLE,
echo     stream_id VARCHAR^(255^),
echo     device_type VARCHAR^(50^),
echo     channel_type INT,
echo     create_time VARCHAR^(50^),
echo     update_time VARCHAR^(50^),
echo     sub_count INT DEFAULT 0,
echo     parental INT DEFAULT 0,
echo     has_audio TINYINT^(1^) DEFAULT 0,
echo     record_plan_id INT
echo ^);
echo.
echo -- 创建录像计划表
echo CREATE TABLE IF NOT EXISTS wvp_record_plan ^(
echo     id INT AUTO_INCREMENT PRIMARY KEY,
echo     snap TINYINT^(1^) DEFAULT 0,
echo     name VARCHAR^(255^) NOT NULL,
echo     create_time VARCHAR^(50^),
echo     update_time VARCHAR^(50^)
echo ^);
echo.
echo -- 创建录像计划项表
echo CREATE TABLE IF NOT EXISTS wvp_record_plan_item ^(
echo     id INT AUTO_INCREMENT PRIMARY KEY,
echo     start INT,
echo     stop INT,
echo     week_day INT,
echo     plan_id INT,
echo     create_time VARCHAR^(50^),
echo     update_time VARCHAR^(50^)
echo ^);
echo.
echo -- 插入默认数据
echo INSERT IGNORE INTO wvp_user_role VALUES ^(1, 'admin', '0', '2021-04-13 14:14:57', '2021-04-13 14:14:57'^);
echo INSERT IGNORE INTO wvp_user VALUES ^(1, 'admin', '21232f297a57a5a743894a0e4a801fc3', 1, '2021-04-13 14:14:57', '2021-04-13 14:14:57', '3e80d1762a324d5b0ff636e0bd16f1e3'^);
echo.
echo -- 显示创建的表
echo SHOW TABLES;
) > wvp_init.sql

echo ✓ SQL脚本已生成: wvp_init.sql

echo.
echo 请手动执行以下命令来初始化数据库：
echo.
echo 方法1: 如果你有MySQL命令行工具
echo mysql -h 127.0.0.1 -P 3306 -u root -p12345678 ^< wvp_init.sql
echo.
echo 方法2: 使用图形化工具（推荐）
echo 1. 打开phpMyAdmin、Navicat、MySQL Workbench等工具
echo 2. 连接到MySQL（127.0.0.1:3306，用户名：root，密码：12345678）
echo 3. 执行 wvp_init.sql 文件中的SQL语句
echo.
echo 方法3: 复制SQL内容手动执行
echo 打开 wvp_init.sql 文件，复制其中的SQL语句到MySQL客户端执行
echo.
echo 执行完成后，重新启动WVP应用程序即可。
echo.
pause
